﻿using Little.Soldier.SWIFT;
using System.Collections.Generic;

namespace Little.Soldier.SWIFT.Validation
{
  public abstract class ValidateTagBase : IValidate
  {
    private bool U0001 = true;
    private string U0002 = string.Empty;
    private List<ValidationRuleBase> U0004 = new List<ValidationRuleBase>();
    private List<SwiftTag> U0003;
    private bool U0005;

    public bool AllowEmptyValue
    {
      get
      {
        return this.U0005;
      }
      set
      {
        this.U0005 = value;
      }
    }

    public virtual bool Result
    {
      get
      {
        return this.U0001;
      }
    }

    public virtual List<ValidationRuleBase> Rules
    {
      get
      {
        return this.U0004;
      }
    }

    internal List<SwiftTag> Tags
    {
      get
      {
        return this.U0003;
      }
      set
      {
        this.U0003 = value;
      }
    }

    internal string TagName
    {
      get
      {
        return this.U0002;
      }
    }

    protected ValidateTagBase(string tagName)
    {
      this.U0002 = tagName;
      this.InitializeRules();
    }

    protected ValidateTagBase(string tagName, bool allowEmptyValue)
    {
      this.U0002 = tagName;
      this.U0005 = allowEmptyValue;
      this.InitializeRules();
    }

    protected abstract void InitializeRules();

    internal void U0002U0002(bool result)
    {
      if (!this.Result)
        return;
      this.U0001 = result;
    }

    private void U0086U0002(ValidationError[] newErrors, List<ValidationError> errorList)
    {
      if (newErrors == null || newErrors.GetLength(0) == 0)
      {
        this.U0002U0002(true);
      }
      else
      {
        foreach (ValidationError validationError in newErrors)
        {
          validationError.ParentPropertyObject = (object) null;
          validationError.PropertyName = this.U0002;
          errorList.Add(validationError);
        }
        this.U0002U0002(false);
      }
    }

    public virtual bool Validate(List<ValidationError> errorList)
    {
      if (errorList == null)
        goto label_7;
label_1:
      string str1 = string.Empty;
      foreach (SwiftTag swiftTag in this.Tags)
      {
        string str2 = swiftTag.Value;
        this.Validate(str2 != null ? ((object) str2).ToString() : string.Empty, errorList);
      }
      return this.U0001;
label_7:
      errorList = new List<ValidationError>();
      goto label_1;
    }

    private bool Validate(string value, List<ValidationError> errorList)
    {
      if (string.IsNullOrEmpty(value) && this.AllowEmptyValue)
        return this.U0001;
      foreach (ValidationRuleBase validationRuleBase in this.Rules)
      {
        string str = value;
        validationRuleBase.U0003U0003(ref str);
        this.U0086U0002(validationRuleBase.Validate(str), errorList);
      }
      return this.U0001;
    }
  }
}
